package com.yyge.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyge.dto.EmployeeDto;
import com.yyge.entity.Employee;
import com.yyge.mapper.EmployeeMapper;
import com.yyge.service.EmployeeService;
import com.yyge.util.MybatisUtil;
import com.yyge.vo.R;

import java.util.List;

public class EmployeeServiceImpl implements EmployeeService {
    @Override
    public R selectAll(EmployeeDto dto) {
        //在进行分页查询前，先通过分页插件进行拦截
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        //开始进行分页查询
        List<Employee> list= MybatisUtil.slect(EmployeeMapper.class, mapper->mapper.selectAll(dto));

        //通过pageInfo从list查询结果中提取总条数
        PageInfo<Employee> pageInfo=PageInfo.of(list);

        //从PageInfo中提取PageNum，......等众多属性封装到pageVo对象中
        //PageVo pageVo=PageVo.of(pageInfo);
        return R.ok(pageInfo);
    }

    @Override
    public R insert(Employee goodsInfo) {
        int rows=MybatisUtil.updata(EmployeeMapper.class,mapper->mapper.insert(goodsInfo));
        return rows>0?R.ok("新增成功！"):R.fail("新增失败！");
    }

    @Override
    public R deleteByPrimaryKey(Integer key) {
        int rows=MybatisUtil.updata(EmployeeMapper.class,mapper->mapper.deleteByPrimaryKey(key));
        return rows>0?R.ok("删除成功！") :R.fail("删除失败！");
    }

    @Override
    public R updateByPrimaryKeySelective(Employee record) {
        int rows=MybatisUtil.updata(EmployeeMapper.class,mapper->mapper.updateByPrimaryKeySelective(record));
        return rows>0?R.ok("修改成功！") :R.fail("修改失败！");
    }

    @Override
    public R updateByPrimaryKey(Employee employee) {
        int rows=MybatisUtil.updata(EmployeeMapper.class,mapper->mapper.updateByPrimaryKey(employee));
        return rows>0?R.ok("修改成功！") :R.fail("修改失败！");
    }

    @Override
    public R selectByPrimaryKey(Integer key) {
        Employee employee =MybatisUtil.slect(EmployeeMapper.class,mapper->mapper.selectByPrimaryKey((long)key));
        return R.ok(employee );
    }
}
